import BuffUtil from '@/BuffUtil';
import RandomUtil from '@/RandomUtil';
import SelectUtil from '@/SelectUtil';
import BuffCommon from 'code/Core/Common/BuffCommon';
import UnitCommon from 'code/Core/Common/UnitCommon';
import AbilityBase from 'code/Scripts/Abilities/base/AbilityBase';
/**
 * 风怒武器
 **/
export default class SM_WindyWeapon extends AbilityBase {
    static instance: SM_WindyWeapon;
    buffType: AppBuffType = {
        id: 'buff_SM_WindyWeapon',
        art: this.art,
        name: this.abilityName,
        ubertip: '增加攻击速度，并在攻击里造成额外风系伤害',
        targetArt: 'Abilities\\Spells\\NightElf\\Cyclone\\CycloneTarget.mdl',
        targetAttach: 'origin',
        dur: 8,
        attribute: {},
    };
    constructor(id: string = 'Afnq') {
        super(id);
        SM_WindyWeapon.instance = this;
        this.init();
        se.onUnitSpellEffect(this.onSpellEffect.bind(this), this.id);
        EventManager.onTakeAttack(this.onAttackEffect.bind(this));
    }
    init() {
        BuffUtil.registerBuffType(this.buffType);
    }
    cast<T extends Object = any>(tarX: number, tarY: number, source: unit, target: unit, data?: T): void {
        let lv = this.getAbilityLevel(source);
        // let buff = BuffUtil.addBuff(source, this.buffType.id, source);
        // buff.attribute.attackSpd_p = 0.15 + 0.15 * lv;
        BuffCommon.addBuff({
            target: source,
            caster: source,
            ablityId: this.id,
            buffTypeId: this.buffType.id,
        });
    }
    onSpellEffect() {
        this.cast(GetSpellTargetX(), GetSpellTargetY(), GetTriggerUnit(), GetSpellTargetUnit());
    }
    onAttackEffect(d: DamageEventData) {
        if (!BuffUtil.hasUnitBuff(d.source, this.buffType.id)) return;
        let r = RandomUtil.randomPercent();
        if (r > 0.6) return;
        let lv = this.getAbilityLevel(d.source);
        let dmg = 30 * lv + UnitCommon.取攻击(d.source) * (0.25 + 0.25 * lv) + UnitCommon.取法强(d.source) * (0.25 + 0.25 * lv);
        let es = SelectUtil.getEnemyUnitsInRange(d.source, 450, GetUnitX(d.target), GetUnitY(d.target));
        let count = es.length > 4 ? 4 : es.length;
        for (let i = 0; i < count; i++) {
            DamageSystemInstance.applyWindDamage(es[i], d.source, dmg, this.id);
            DestroyEffect(AddSpecialEffectTarget('Abilities\\Spells\\NightElf\\Cyclone\\CycloneTarget.mdl', es[i], 'origin'));
        }
    }
}
